package cron

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/util/gconv"
	"reflect"
)

// toITime 求FixTime所有字段的笛卡尔积
func (f *FixTime) toITime() (t [][]uint) {
	//m 的 01234 分别对应 MWHMS

	m := make([][]uint, 0)
	val := reflect.ValueOf(*f)
	for i := 0; i < val.NumField(); i++ {
		if len(val.Field(i).Interface().([]uint)) == 0 {
			m = append(m, []uint{})
			continue
		}
		gArray := garray.NewArrayFrom(gconv.Interfaces(val.Field(i).Interface().([]uint)))
		gArray.SortFunc(func(v1, v2 interface{}) bool {
			if v1.(uint) < v2.(uint) {
				return true
			} else {
				return false
			}
		})
		tempUint := make([]uint, 0)
		gArray.Iterator(func(k int, v interface{}) bool {
			tempUint = append(tempUint, v.(uint))
			return true
		})
		m = append(m, tempUint)
	}

	lens := func(i int) int { return len(m[i]) }
	for ix := make([]int, len(m)); ix[0] < lens(0); NextIndex(ix, lens) {
		var r []uint
		for j, k := range ix {
			r = append(r, m[j][k])
		}
		t = append(t, r)
	}
	fmt.Println("m", m)
	return m
}

func NextIndex(ix []int, lens func(i int) int) {
	for j := len(ix) - 1; j >= 0; j-- {
		ix[j]++
		if j == 0 || ix[j] < lens(j) {
			return
		}
		ix[j] = 0
	}
}
